import logging
import time
import os

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s')
logFormatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
rootLogger = logging.getLogger()

lctime = time.localtime() # 本地时间
lctime = time.strftime("%Y-%m-%d_%A_%H:%M:%S",lctime) # 格式化时间

if not os.path.exists("log"): # 判断是否存在log文件夹
    os.makedirs("log") # 创建log文件夹

# 日志文件的命名，w+ 表示可读可写，如果文件不存在则创建，如果文件存在则覆盖
fileHandler = logging.FileHandler("log/" + "test" + ".log", 'w+') 
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)

logging.info("Start print log")
logging.debug("Do something")
logging.warning("Something maybe fail.")
logging.info("Finish")
